/*
    33. Write a C program to display Pascal's triangle. 
    Test Data :
    Input number of rows: 5
    Expected Output :

            1
          1   1 
        1   2   1 
      1   3   3   1
    1   4   6   4   1 
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    int rows, cols; 
    printf("Input number of rows: ");
    scanf("%d", &rows);
    cols = 2 * rows - 1;
    int *curRow = (int *)malloc(cols * sizeof(int));
    int *nextRow = (int *)malloc(cols * sizeof(int));
    memset(curRow, 0, cols * sizeof(int));
    memset(nextRow, 0, cols * sizeof(int));
    curRow[cols/2] = 1;
    for(int j = 0; j < rows; j++){
        for(int i = 0; i < cols; i++){
            if(curRow[i] == 0){
                printf(" ");
            }else{
                printf("%d", curRow[i]);
            }
            
            if(i == 0){
                nextRow[i] = curRow[1];
            }else if(i == cols - 1){
                nextRow[i] = curRow[cols - 2];
            }else{
                nextRow[i] = curRow[i - 1] + curRow[i + 1];                
            }            
        }
        printf("\n");
        memcpy(curRow, nextRow, (cols * sizeof(int)));
    }
    
    free(curRow);
    free(nextRow);
     
    return 0;
}